
package com.theobserver.theobserver;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.GetCallback;


public class UpdateActivity extends AppCompatActivity {

    private static final String TAG = "ActivityDemo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        TextView tv_currentversion=(TextView)findViewById(R.id.tv_currentversion);
        tv_currentversion.setText("当前版本号："+ CurrentVersion.version);



        AVObject update = AVObject.createWithoutData("update", "5963822b1b69e64c8d296e06");
        update.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                final int lc_version = avObject.getInt("update_version");
                final String lc_build = avObject.getString("build_version");
                final String lc_description = avObject.getString("update_description");
                final String lc_target = avObject.getString("update_target");



                if ((CurrentVersion.version)==(lc_version)){

                    LinearLayout up_no=(LinearLayout) findViewById(R.id.linearlayout_add);
                    View view=View.inflate(UpdateActivity.this, R.layout.update_no,null);
                    up_no.addView(view);

                }
                else
                {
                    LinearLayout up_yes=(LinearLayout) findViewById(R.id.linearlayout_add);
                    View view=View.inflate(UpdateActivity.this, R.layout.update_yes,null);
                    up_yes.addView(view);

                    TextView uv=(TextView)findViewById(R.id.tv_uv);
                    uv.setText(""+lc_version);

                    TextView bv=(TextView)findViewById(R.id.tv_bv);
                    bv.setText(lc_build);

                    TextView ud=(TextView)findViewById(R.id.tv_ud);
                    ud.setText(lc_description);

                }

                Button bt_update=(Button)findViewById(R.id.bt_update);
                bt_update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if ((CurrentVersion.version)==(lc_version))
                            Toast.makeText(UpdateActivity.this, "已是最新版本，无需更新！" , Toast.LENGTH_SHORT).show();
                        else
                        {
                            Uri uri = Uri.parse(lc_target);
                            Intent it = new Intent();
                            it.setAction(Intent.ACTION_VIEW);
                            it.setData(uri);
                            startActivity(it);

                        }
                    }
                });
            }
        });


        //返回按钮
        TextView tv_reten=(TextView)findViewById(R.id.tv_update_reten);
        tv_reten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
